package stack;

public class Calculator {

    public static void main(String[] args) {
        String exp = "3+2*6-2";
        ArrayStack1 numStack = new ArrayStack1(10);
        ArrayStack1 operStack = new ArrayStack1(10);

        int index = 0;//用于扫描
        int num1 = 0;
        int num2 = 0;
        int oper = 0;
        int res = 0;
        char ch = ' ';//每次扫描的char保存进来
        //开始遍历
//        while (true){
//            //一次得到exp的每个字符
//            ch = exp.substring(index,index+1).charAt(0);
//            if (numStack.isOper(ch)){
//                operStack.push(ch);
//            }else {
//                numStack.push(ch);
//            }
//        }
        for (int i = 0; i < exp.length(); i++){
            ch = exp.substring(i,i+1).charAt(0);
            if (numStack.isOper(ch)) {
                if (operStack.isEmpty()){//如果数组为空可以加
                    operStack.push(ch);

                }else { //不为空判断当前运算符与栈中运算符优先级
                    if (operStack.priority(ch) <= operStack.priority(operStack.peek())){//如果当前栈顶的运算符优先级高于当前遍历到的
                        //那么就将栈顶的运算符给抽取出来先做运算
                        num1 = numStack.pop();
                        num2 = numStack.pop();
                        oper = operStack.pop();
                        res = numStack.cal(num1, num2, oper);
                        numStack.push(res);
                        operStack.push(ch);
                    }else {
                        operStack.push(ch);
                    }
                }
            }else {
                numStack.push(ch - 48);
            }
        }
        //运算
        while (true){
            //如果符号栈为空数字栈只有一个那就是结果
            if (operStack.isEmpty()){
                break;
            }
            num1 = numStack.pop();
            num2 = numStack.pop();
            oper = operStack.pop();
            res = numStack.cal(num1, num2, oper);
            numStack.push(res);
        }
        System.out.println(numStack.pop());

    }
}

//先创建一个栈
//用数组Array模拟栈
class ArrayStack1{
    private int maxSize;//栈的最大深度
    private int[] stack;//栈的数据
    private int top = -1;//栈顶，当为-1时表示栈中没有元素

    public ArrayStack1(int maxSize){
        this.maxSize = maxSize;
        stack = new int[this.maxSize];
    }
    //返回当前栈顶的元素，并非弹出
    public int peek(){
        return stack[top];
    }

    //判断栈满
    public boolean isFull(){
        return top == maxSize - 1;
    }

    //判断栈空
    public boolean isEmpty(){
        return top == -1;
    }

    //入栈
    public void push(int i){
        if (!isFull()){
            stack[++top] = i;
        }
    }

    //出栈
    public int pop(){
        if (!isEmpty()){
            return stack[--top < 0 ? 0:top];
        }
        return 0;
    }

    //遍历
    public void list(){
        if (isEmpty()){
            return;
        }
        for (int j = top; j >= 0; j--){
            System.out.println(stack[j]);
        }
    }

    //返回运算符优先级
    public int priority(int oper){
        if (oper == '*' || oper == '/'){
            return 1;
        }else if (oper == '+' || oper == '-'){
            return 0;
        }else {
            return -1;
        }
    }

    //判断是不是运算符
    public boolean isOper(char val){
        return val == '+' || val == '-' || val == '*' ||val == '/';
    }

    //计算方法
    public int cal(int num1, int num2, int oper){
        int res = 0;
        switch (oper){
            case '+':
                res = num1 + num2;
                break;
            case '-':
                res = num1 - num2;
                break;
            case '*':
                res = num1 * num2;
                break;
            case '/':
                res = num1 / num2;
                break;
            default:
                res = 0;
                System.out.println("错误");
        }
        return res;
    }
}
